﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace HYSoft.Web
{
    public partial class monitorfp : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["uid"] == null || Session["uid"] == "")
            {
                Context.Response.Write("<script language=\"javascript\" type=\"text/javascript\">");
                Context.Response.Write("alert('对不起,你登录时间超时,请重新登录');top.location.href=\"index.html\";");
                Context.Response.Write("</script>");
                Context.Response.End();
            }
            if (Request.QueryString["touid"] == null)
                uidnull();
            else
            {
                if (!IsPostBack)
                {
                    user_list();
                    monitor_list();
                    monitor2user();
                    clritem();
                }
                nodetype_change();
            }
        }
        protected void btAddOne_Click(object sender, EventArgs e)
        {
            int index = lstAll.Items.Count;
            for (int i = 0; i < index; i++)
            {
                if (lstAll.SelectedIndex == i)
                {
                    lstSelected.Items.Add(lstAll.SelectedItem);
                    lstAll.Items.RemoveAt(i);
                }
            }
        }
        protected void btnDelOne_Click(object sender, EventArgs e)
        {
            int index = lstSelected.Items.Count;
            for (int i = 0; i < index; i++)
            {
                if (lstSelected.SelectedIndex == i)
                {
                    lstAll.Items.Add(lstSelected.SelectedItem);
                    lstSelected.Items.RemoveAt(i);
                }
            }
        }
        protected void btnAddAll_Click(object sender, EventArgs e)
        {
            int i = 0;
            foreach (ListItem item in lstAll.Items)
            {
                lstSelected.Items.Insert(i, item);
                i = i + 1;
            }
            lstAll.Items.Clear();
        }
        protected void btnDelAll_Click(object sender, EventArgs e)
        {
            int i = 0;
            foreach (ListItem item in lstSelected.Items)
            {
                lstAll.Items.Insert(i, item);
                i = i + 1;
            }
            lstSelected.Items.Clear();
        }
        private void monitor_list()
        {
            string sql = " [user_index]=" + Session["uid"] + "";
            DataSet ds = new BLL.usr2monitor().GetList(sql);

            lstAll.DataSource = ds.Tables[0].DefaultView;
            lstAll.DataTextField = "Addr";//指定下拉列表中的文字显示部分 
            lstAll.DataValueField = "Index";//指定下拉列表中的值部分 
            //DataTable类实现了IListSource接口 
            lstAll.DataBind();
            ds.Clear();
            ds.Dispose();
        }
        private void monitor2user()
        {

            string sql = " [user_index]=" + Request.QueryString["touid"].ToString() + "";
            DataSet ds = new BLL.usr2monitor().GetList(sql);
            lstSelected.DataSource = ds.Tables[0].DefaultView;
            lstSelected.DataTextField = "Addr";//指定下拉列表中的文字显示部分 
            lstSelected.DataValueField = "Index";//指定下拉列表中的值部分 
            //DataTable类实现了IListSource接口 
            lstSelected.DataBind();
            ds.Clear();
            ds.Dispose();
        }
        private void user_list()
        {
            string units1 = "";

            string sql = " [index]=" + Session["uid"] + "";
            DataSet ds = new BLL.user().GetList(sql);
            units1 = ds.Tables[0].Rows[0]["unit"].ToString();
            ds.Clear();
            ds.Dispose();

            sql = " [unit]='" + units1 + "' and [type]=0 and [Intvalue2]=1";
            DataSet ds2 = new BLL.user().GetList(sql);
            if (ds2.Tables[0].Rows.Count > 0)
            {
                DropDownList1.DataSource = ds2.Tables[0].DefaultView;
                DropDownList1.DataTextField = "name";
                DropDownList1.DataValueField = "index";
                DropDownList1.DataBind();
                ds2.Clear();
                ds2.Dispose();
                DropDownList1.SelectedValue = Request.QueryString["touid"].ToString();
            }
        }
        protected void nodetype_change()
        {
            string nodetype = DropDownList1.SelectedValue.ToString();
            if (nodetype != Request.QueryString["touid"].ToString())
                Response.Write("<script>window.location = 'monitorfp.aspx?touid=" + nodetype + "';</script>");
        }

        private void clritem()
        {
            foreach (ListItem item in lstSelected.Items)
            {
                if (lstAll.Items.Contains(item))
                {
                    int index = this.lstAll.Items.IndexOf(item);
                    this.lstAll.Items.RemoveAt(index);
                }
            }
        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            string sql = " [user_index]=" + DropDownList1.SelectedValue.ToString() + "";
            DataSet ds3 = new BLL.usr2monitor().GetList(sql);
            foreach (ListItem item in lstSelected.Items)
            {
                int flag1 = 0;
                for (int i = 0; i < ds3.Tables[0].Rows.Count; i++)
                {
                    if (item.Value == ds3.Tables[0].Rows[i]["index"].ToString())
                        flag1 = 1;
                }
                if (flag1 == 0)
                {
                    Model.monitor_to_user mtou = new Model.monitor_to_user();
                    mtou.user_index = int.Parse(DropDownList1.SelectedValue.ToString());
                    mtou.monitor_index = int.Parse(item.Value);
                    new BLL.monitor_to_user().Add(mtou);
                }
            }

            for (int i = 0; i < ds3.Tables[0].Rows.Count; i++)
            {
                int flag2 = 0;
                foreach (ListItem item in lstSelected.Items)
                {
                    if (ds3.Tables[0].Rows[i]["index"].ToString() == item.Value)
                        flag2 = 1;
                }
                if (flag2 == 0)
                {
                    string sql1 = " user_index='" + DropDownList1.SelectedValue.ToString() + "' and monitor_index='" + ds3.Tables[0].Rows[i]["index"].ToString() + "'";
                    new BLL.monitor_to_user().DelWhere(sql1);
                }

            }

            Response.Write("<script>alert('更新成功');window.location = 'monitorfp.aspx?touid=" + DropDownList1.SelectedValue.ToString() + "';</script>");

        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Write("<script>window.location = 'monitorfp.aspx?touid=" + DropDownList1.SelectedValue.ToString() + "';</script>");
        }

        private void uidnull()
        {
            string units1 = "";

            string sql = " [index]=" + Session["uid"] + "";
            DataSet ds3 = new BLL.user().GetList(sql);
            units1 = ds3.Tables[0].Rows[0]["unit"].ToString();
            ds3.Clear();
            ds3.Dispose();

            sql = " [unit]='" + units1 + "' and [type]=0 and [Intvalue2]=1";
            DataSet ds2 = new BLL.user().GetList(sql);
            if (ds2.Tables[0].Rows.Count > 0)
            {
                Response.Write("<script>window.location = 'monitorfp.aspx?touid=" + ds2.Tables[0].Rows[0]["index"] + "';</script>");
            }
            else
            {
                Response.Write("<script>alert('该单位还没有已审批普通用户！');history.back();</script>");
            }
        }
        protected void lstSelected_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstAll.SelectedIndex = -1;
        }
        protected void lstAll_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstSelected.SelectedIndex = -1;
        }
        protected void Button4_Click(object sender, EventArgs e)
        {
            Response.Write("<script>window.location = 'monitor_add.aspx';</script>");
        }
        protected void Button3_Click(object sender, EventArgs e)
        {
            if (lstAll.SelectedIndex == -1)
            {
                Response.Write("<script>alert('请先在\"本单位所属监测点\"列表中选中要删除的监测点，然后才点击\"删除\"按钮进行监测点删除！');history.back();</script>");
            }
            else
            {
                int gid = int.Parse(lstAll.SelectedValue);
                new BLL.monitor().Delete(gid);
                string s = " [monitor_index]=" + gid + "";
                new BLL.monitor_to_user().DelWhere(s);
                s = " areano=" + gid + "";
                //判断该监测点下是否有节点，如果存在就删除，没有就不需要
                try
                {
                    DataSet ds = new BLL.gprsNode().GetList(s);
                    Model.gprsNode gprsnode = new BLL.gprsNode().GetModel(int.Parse(ds.Tables[0].Rows[0]["index"].ToString()));
                    gprsnode.areano = -1;
                    new BLL.gprsNode().Update(gprsnode);
                    Response.Write("<script>alert('删除监测点成功！所属传感器节点已全释放，请重新分配！');window.location = 'monitorfp.aspx';</script>");
                }
                catch(Exception w){
                    Response.Write("<script>alert('删除监测点成功！');window.location = 'monitorfp.aspx';</script>");
                }
                //da.deltable("monitor", " [index]=" + gid);
                //da.deltable("monitor_to_user", " [monitor_index]=" + gid);
                //da.updatatable("gprsNode", " areano=-1   where areano=" + gid);
                }
        }
    }
}